<!DOCTYPE html>
<!--注册页面-->
<html class="ui-page-login">
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="css/mui.min.css" rel="stylesheet" />
		<link href="css/style.css" rel="stylesheet" />
		<style>
			.area {
				margin: 20px auto 0px auto;
			}
			
			.mui-input-group:first-child {
				margin-top: 20px;
			}
			
			.mui-input-group label {
				width: 22%;
			}
			
			.mui-input-row label~input,
			.mui-input-row label~select,
			.mui-input-row label~textarea {
				width: 78%;
			}
			
			.mui-checkbox input[type=checkbox],
			.mui-radio input[type=radio] {
				top: 6px;
			}
			
			.mui-content-padded {
				margin-top: 25px;
			}
			
			.mui-btn {
				padding: 10px;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">注册</h1>
		</header>
		<div class="mui-content">
			<form class="mui-input-group">
				<div class="mui-input-row">
					<label>账号</label>
					<input id='account' type="text" class="mui-input-clear mui-input" placeholder="请输入账号" />
				</div>
				<div class="mui-input-row">
					<label>密码</label>
					<input id='password' type="password" class="mui-input-clear mui-input" placeholder="请输入密码" />
				</div>
				<div class="mui-input-row">
					<label>确认</label>
					<input id='password_confirm' type="password" class="mui-input-clear mui-input" placeholder="请输入密码" />
				</div>
				<div class="mui-input-row">
					<label>手机</label>
					<input id='checkPhone' type="text" class="mui-input-clear mui-input" placeholder="请输入手机号" />
				</div>
			</form>
			<div class="mui-row">
				<div class="mui-col-sm-7 mui-col-xs-8">
					<input id="code" type="text" class="mui-input-clear mui-input " placeholder="请输入您接收到的验证码">
				</div>
				<div class="mui-col-sm-5 mui-col-xs-4">
					<div style="text-align: right;">
						<button id="renew" style="height: 40px;text-align: top;"><span id="count">重新发送</span></button>
					</div>
				</div>
			</div>
			<div class="mui-row">
				<div class="mui-col-sm-1 mui-col-xs-2"></div>
				<div class="mui-col-sm-1 mui-col-xs-2">
					<div class="mui-input-row mui-checkbox mui-left" style="height: 40px;">
						<label style="">
							<a style="color: black;" href="html/legal.html"></a>
						</label>
						<input class="checkbox" type="checkbox"></input>
					</div>
				</div>
				<div class="mui-col-sm-9 mui-col-xs-7" style="line-height: 40px;">
					<a style="color: black;font-size: 12px;" href="html/legal.html">点击同意条款完成注册</a>
				</div>
			</div>
			<div class="mui-content-padded">
				<button id='reg' class="mui-btn mui-btn-block mui-btn-primary">注册</button>
			</div>
			<div class="mui-content-padded">
				<div class="mui-row">
					<div class="mui-col-sm-12 mui-col-xs-12">
						<div style="margin: 5px;">
							<h4>说明：</h4>
							<p>
								发送给您的验证码有效期只用3分钟，过期请点击重新发送!!
							</p>
						</div>
					</div>
				</div>
			</div>
		</div>
		<script src="js/mui.min.js"></script>
		<script src="js/app.js"></script>
		<script src="js/myjs/reg.js"></script>
		<script src="js/ipjs/ip.js"></script>
		<script src="js/constatnt/var.js"></script>
		<script src="js/myjs/localVarchar.js"></script>
		<script>
			//当前手机号
			var isPhone;
			//阅读条款
			var regRead = "false";
			mui('.mui-checkbox').on('change', 'input', function() {
				var value = this.checked ? "true" : "false";
				regRead = value;
			});
			(function($, doc) {
				$.init();
				$.plusReady(function() {
					var settings = app.getSettings();
					var regButton = doc.getElementById('reg');
					var accountBox = doc.getElementById('account');
					var passwordBox = doc.getElementById('password');
					var passwordConfirmBox = doc.getElementById('password_confirm');
					//验证手机号
					var checkPhone = doc.getElementById('checkPhone');
					regButton.addEventListener('tap', function(event) {
						var regInfo = {
							account: accountBox.value,
							password: passwordBox.value,
							phone: checkPhone.value
						};
						var passwordConfirm = passwordConfirmBox.value;
						if(passwordConfirm != regInfo.password) {
							plus.nativeUI.toast('密码两次输入不一致');
							return;
						}
						//逻辑业务判断
						app.reg(regInfo, function(err) {
							//验证码
							var code = document.getElementById("code").value;
							var phone = document.getElementById("checkPhone").value;
							if(err) {
								plus.nativeUI.toast(err);
								return;
							}
							//自定义逻辑判断
							if(regRead.trim() == "false") {
								mui.toast("您需要同意条款");
								isRedRead = false;
								return;
							} else if(regRead.trim() == "true") {
								//mui.toast("同意条款");
								isRedRead = true;
							}
							if(phone == "") {
								mui.toast("手机号不能为空！ ");
								return;
							}
							//验证码判断
							if(code == "") {
								mui.toast("请输入验证码！")
								return;
							} else {
								//执行注册逻辑
								//构造json字符串
								var userInfo = userEntity(regInfo.account, regInfo.password, phone, code, isRedRead);
								if(isRedRead && istel(phone) && code != "") {

									//请求服务器判断验证信息
									mui.ajax(ip + '/userRegister/register', {
										data: JSON.stringify(userInfo),
										dataType: 'json', //服务器返回json格式数据
										type: 'post', //HTTP请求类型
										timeout: 10000, //超时时间设置为10秒；
										headers: {
											'Content-Type': 'application/json'
										},
										success: function(data) {
											//服务器返回响应，根据响应结果，分析是否登录成功；
											status = data.status;
											desc = data.data.desc;
											if(USER_EXIST === data.data.code) {
												mui.toast("用户已存在！");
												return;
											} else if(CODE_ERROR === data.data.code) {
												mui.toast("验证码错误");
												return;
											} else if(IS_ARGGER === data.data.code) {
												mui.toast("您未同意条款");
												return;
											}
											if(status == OPERATION_SUCCESS) {
												plus.nativeUI.toast(desc);
												//存储本地信息(改进：后期加密+token验证)
												localStorage.setItem(LOCAL_USER_INFO, isPhone);
												//跳转到登录页
												$.openWindow({
													url: 'index-homePage.html',
													id: 'homePage',
													extras: {
														//需要传递的值
														localPhone: isPhone
													}
												});
											} else {
												plus.nativeUI.toast(desc);
											}

										},
										error: function(xhr, type, errorThrown) {
											//异常处理；
											mui.toast("系统异常！");
										}
									});
								} else {
									mui.toast("您输入的信息不正确！");
								}
							}
						});

					});
				});
			}(mui, document));
			var flag = true;
			document.getElementById("renew").addEventListener('tap', function() {

				if(document.getElementById("checkPhone").value != "" && isPhone != "") {
					if(isPhone == "" || isPhone == null) {
						isPhone = document.getElementById("checkPhone").value;
					}
					mui.ajax(ip + '/send/code/' + isPhone, {
						type: 'get', //HTTP请求类型
						timeout: 10000, //超时时间设置为10秒；
						success: function(data) {},
						error: function(xhr, type, errorThrown) {
							//异常处理；
							mui.toast("验证码发送失败！");
						}
					});
				} else if(isPhone == null) {
					mui.toast("手机号不能为空");
					return;
				}
				if(flag) {
					var count = 120;
					var timer1 = setInterval(function() {
						if(count <= 0) {
							flag = true;
							document.getElementById("count").innerHTML = "重新发送";
							//执行发送逻辑
							window.clearInterval(timer1);
						} else {
							flag = false;
							count = count - 1;
							document.getElementById("count").innerHTML = count + "s后发送重新";
						}
					}, 1000);
				}
			});

			document.getElementById("checkPhone").addEventListener('blur', function() {
				if(istel(this.value)) {
					isPhone = document.getElementById("checkPhone").value;
				} else {
					this.value = "";
				}

			});

			function istel(tel) {
				var rtn = false;
				//移动号段
				var regtel = /^((13[4-9])|(15([0-2]|[7-9]))|(18[2|3|4|7|8])|(178)|(147))[\d]{8}$/;
				if(regtel.test(tel)) {
					rtn = true;
				}
				//电信号段
				regtel = /^((133)|(153)|(18[0|1|9])|(177))[\d]{8}$/;
				if(regtel.test(tel)) {
					rtn = true;
				}
				//联通号段
				regtel = /^((13[0-2])|(145)|(15[5-6])|(176)|(18[5-6]))[\d]{8}$/;
				if(regtel.test(tel)) {
					rtn = true;
				}
				return rtn;
			}
			/*				mui.ajax('http://10.10.2.215:8090/userRegister/test', {
								type: 'get', //HTTP请求类型
								timeout: 10000, //超时时间设置为10秒；
								success: function(data) {
									//服务器返回响应，根据响应结果，分析是否登录成功；
									alert(data.status+"--"+data.data);
								},
								error: function(xhr, type, errorThrown) {
									//异常处理；
									mui.toast("请求失败！");
								}
							});*/
		</script>
	</body>

</html>